package com.tnt.hpit.helper;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector3;
import com.tnt.hpit.shared.Constants;

public class OrthoCamController extends InputAdapter {
	final OrthographicCamera camera;
	final Vector3 curr = new Vector3();
	final Vector3 last = new Vector3(-1, -1, -1);
	final Vector3 delta = new Vector3();
	private Vector3 v3ToaDoManHinh = new Vector3();
	
	public OrthoCamController(OrthographicCamera camera) {
		this.camera = camera;
	}
	
	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		v3ToaDoManHinh.set(x, y, 0);
		camera.unproject(v3ToaDoManHinh);
//		Gdx.app.log("TNT Touch Down", String.valueOf(v3ToaDoManHinh.x) + " " + String.valueOf(v3ToaDoManHinh.y));
//		System.out.println("Touch Down");
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		camera.unproject(curr.set(x, y, 0)); //lay toa do the gioi cua curr.
		if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
			camera.unproject(delta.set(last.x, last.y, 0));
			delta.sub(curr);
			camera.position.add(delta.x, delta.y, 0);
		}
		last.set(x, y, 0);
		
		if ( camera.position.y < Constants.SCREEN_HEIGHT / 2 )
			camera.position.y = Constants.SCREEN_HEIGHT / 2;
		if ( camera.position.x < Constants.SCREEN_WIDTH / 2 )
			camera.position.x = Constants.SCREEN_WIDTH / 2;
		if ( camera.position.x > Constants.SCREEN_PLAY_WIDTH - Constants.SCREEN_WIDTH / 2)
			camera.position.x = Constants.SCREEN_PLAY_WIDTH - Constants.SCREEN_WIDTH / 2;
		if ( camera.position.y > Constants.SCREEN_PLAY_HEIGHT - Constants.SCREEN_HEIGHT / 2)
			camera.position.y = Constants.SCREEN_PLAY_HEIGHT - Constants.SCREEN_HEIGHT / 2;
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		last.set(-1, -1, -1);
		return false;
	}
}